/**
 * Created by rjx0801 on 16/4/1.
 */

// var line1 = [['Cup Holder Pinion Bob', 7], ['Generic Fog Lamp', 9], ['HDTV Receiver', 15],
//        ['8 Track Control Module', 12], [' Sludge Pump Fourier Modulator', 3],
//        ['Transcender/Spice Rack', 6], ['Hair Spray Danger Indicator', 18]];

$(function (){

    //分页
    var pager=$('#detailGrid').datagrid('getPager');
    pager.pagination({
        pageSize:10,
        pageList : [10,20],// 可以设置每页记录条数的列表
        beforePageText: '第',//页数文本框前显示的汉字 
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        onBeforeRefresh: function () {
        },
        onSelectPage: function (pageNumber, pageSize) {//分页触发
            getGridData(pageNumber, pageSize);
        }
    });

    var occurredOn;
    var tmpArr, QueryString;
    var URL = document.location.toString(); //获取带参URL
    if (URL.lastIndexOf("?") != -1) {
        QueryString = URL.substring(URL.lastIndexOf("=") + 1, URL.length);
        $('#iccid').val(QueryString);
        $('#detailGrid').datagrid({
            url: "/dtm/dayCardDetailListByIccid",
            queryParams: {
                occurredOn: occurredOn,
                iccid: QueryString
            }
        });
    }

    $("#occurredOn").datetimepicker({

        startView:"3",    //日期时间选择器打开之后首先显示的视图。 可接受的值：
        minView:"3",         //日期时间选择器所能够提供的最精确的时间选择视图。
        todayHighlight:"true",  //如果为true, 高亮当前日期
        format: "yyyy-mm", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose:true //选择日期后自动关闭

    });

});

//ajax调用获取数据
function getGridData (pageNumber, pageSize) {

    $.ajax({
        type: "POST",
        dataType: "json",
        url: '/dtm/dayCardDetailList',
        data: {
            'page': pageNumber,
            'rows': pageSize
        },
        success: function (data) {
            $("#detailGrid").datagrid('loadData', pageData(data.rows, data.total));//用来封装获取的总条数，和数据，data.rows是我在控制器里面添加的一个map集合的键的名称
        },
        error: function (err) {
            alert("失败");
        }
    });

}

function pageData(list,total){
    var obj=new Object();
    obj.total=total;
    obj.rows=list;
    return obj;
}

function formatDatebox(value) {
    if (value == null || value == '') {
        return '';
    }

    var year=parseInt(value.year)+1900;
    var month=(parseInt(value.month)+1);
    month=month>9?month:('0'+month);
    var date = (parseInt(value.date));
    date = date>9?date:('0'+date);
    // var hours=parseInt(value.hours);
    // hours=hours>9?hours:('0'+hours);
    // var minutes=parseInt(value.minutes);
    // minutes=minutes>9?minutes:('0'+minutes);
    // var seconds=parseInt(value.seconds);
    // seconds=seconds>9?seconds:('0'+seconds);
    var time=year+'-'+month+'-'+date; //+' '+hours+':'+minutes+':'+seconds;
    return time;
}

function dtmSearch() {

    var iccid = $('#iccid').val();
    // var occurredOn = $('#occurredOn').datebox('getValue');

    var occurredOn = $('#occurredOn').val();

        // .datebox('getValue');

    var reg = /^(\d{4}|\d{2})-(0?[1-9]|1[012])$/;

    if((iccid.length == 0 || iccid.substring(0, iccid.length) == 0)
        && (occurredOn.length == 0 || occurredOn.substring(0, occurredOn.length) == 0)) {

        alert("输入需要查询的条件不能为空或空格");

        $('#detailGrid').datagrid({
            url: "/dtm/dayCardDetailList",
            queryParams: {
                occurredOn: occurredOn,
                iccid: iccid
            }
        });

    }else if(!reg.test(occurredOn) && iccid.length == 0){

        alert("输入需要查询的时间有误");

    }else if(iccid.length > 0 && occurredOn.length == 0){

        $('#detailGrid').datagrid({
            url: "/dtm/dayCardDetailList",
            queryParams: {
                occurredOn: occurredOn,
                iccid: iccid
            }
        });


    }else if(iccid.length == 0 && occurredOn.length > 0){

        // alert("时间查询");
        $('#detailGrid').datagrid({
            url: "/dtm/dayCardDetailList",
            queryParams: {
                occurredOn: occurredOn,
                iccid: iccid
            }
        });


    }else if(iccid.length > 0 && occurredOn.length > 0){

         // alert("双向查询");
        $('#detailGrid').datagrid({
            url: "/dtm/dayCardDetailList",
            queryParams: {
                occurredOn: occurredOn,
                iccid: iccid
            }
        });
        
        drawing(iccid,occurredOn);
    }

}

function drawing(iccid,time) {

    $.ajax({
        type: "POST", //提交的类型
        dataType: "json",
        url: "/dtm/getInfoByMonth/", //提交地址
        data: {iccid: iccid, occurredOn: time},
        success: function (msg) {
            var dataValue = new Array(); //数据
            var ticks = new Array(); //横坐标值
            $(msg).each(function (index, value) {
                dataValue.push(msg[index].traffic);
                ticks.push(msg[index].occurredDate);
            });

            $("#chart").html("");
            var plot1 = $.jqplot('chart', [dataValue], {
                animate: !$.jqplot.use_excanvas, //是否动画显示
                seriesDefaults: {
                    renderer: $.jqplot.BarRenderer, // 利用渲染器（BarRenderer）渲染现有图表
                    pointLabels: {show: true},
                    rendererOptions: {
                         barWidth: 20
                    }
                },
                title: "流量明细柱状图", //标题
                axesDefaults: {
                    tickRenderer:$.jqplot.CanvasAxisTickRenderer,
                },
                axes: {
                    xaxis: {
                        renderer: $.jqplot.CategoryAxisRenderer, // 设置横（纵）轴上数据加载的渲染器
                        ticks: ticks,
                        tickOptions: {
                            fontFamily: 'Georgia',
                            angle: -30,
                            fontSize: '8pt',
                            labelPosition: 'start',
                            tickDistribution: 'even'
                        }
                    },
                    yaxis: {
                        tickOptions: {formatString: '%.2f(M)'}// 设置坐标轴上刻度值显示格式
                    }
                },
                highlighter: {
                    show: true,
                    sizeAdjust: 0,
                    lineWidthAdjust: 0,
                    tooltipContentEditor: tooltipContentEditor
                }
            });
        }
    });

}


function tooltipContentEditor(str, seriesIndex, pointIndex, plot) {
    // 显示series_label, x-axis_tick, y-axis 数据
    return plot.data[seriesIndex][pointIndex];
}
